//
//  ZZCRegisterThreeController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/21.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCRegisterThreeController.h"
#import "ZZCRegisterFourController.h"

@interface ZZCRegisterThreeController ()

@end

@implementation ZZCRegisterThreeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"正资产账户注册";
    [self.passwordTextField becomeFirstResponder];
    [self.passwordTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.passwordTextField.secureTextEntry = YES;
    self.passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
//下一步
- (IBAction)nextClick {
    //    判断密码
    if (![ZZCUtils checkPassword:self.passwordTextField.text]) {
        NSLog(@"密码格式错误");
        [SVProgressHUD showInfoWithStatus:@"密码只能由字母数字下划线组成(6-16位)"];
        return;
    }

    ZZCRegisterFourController *four = [ZZCRegisterFourController new];
    four.password = self.passwordTextField.text;
    four.phone = self.phone;
    four.openId = self.openId;
    four.iconUrl = self.iconUrl;
    [self.navigationController pushViewController:four animated:YES];
}

- (void)textFieldDidChange:(UITextField *)textField{
    UITextRange *selectedRange = textField.markedTextRange;
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    if (!position) {
        // 没有高亮选择的字
        // 1. 过滤非汉字、字母、数字字符
//        self.passwordTextField.text = [self filterCharactor:textField.text withRegex:@"[^a-zA-Z0-9\u4e00-\u9fa5]"];
        // 2. 截取
        if (self.passwordTextField.text.length >= 16) {
            self.passwordTextField.text = [self.passwordTextField.text substringToIndex:16];
        }
    } else {
        // 有高亮选择的字 不做任何操作
    }
}
// 过滤字符串中的非汉字、字母、数字
- (NSString *)filterCharactor:(NSString *)string withRegex:(NSString *)regexStr{
    NSString *filterText = string;
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error];
    NSString *result = [regex stringByReplacingMatchesInString:filterText options:NSMatchingReportCompletion range:NSMakeRange(0, filterText.length) withTemplate:@""];
    return result;
}
// return键按下
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    if (textField.text.length < 2) {
        // 提示用户，名称长度至少为2位
        return NO;
    }
    // 设置用户名
    return YES;
}

@end
